﻿/** Chargement d'un autre personnage.
 * @param name Nom du personnage
 * @param data Contient la position, le quaternion et l'animation en cours */
function loadOtherCharacter(name, data) {
	var loaderChar = new THREE.JSONLoader();
	// OTHER CHARACTER
	loaderChar.load('/models/male.js', function(geometry) {
		var material = new THREE.MeshPhongMaterial({
				color: 0xffffff, 
				specular: 0x333333, 
				shininess: 20, 
				wrapAround: true, 
				morphTargets: true, 
				morphNormals: true,
				vertexColors: THREE.NoColors, 
				shading: THREE.SmoothShading, 
				map: new GT.Texture('/textures/male_texture.png')
			},{
				color: 0xffffff,
				specular: 0x333333, 
				shininess: 20, 
				wrapAround: true, 
				morphTargets: true, 
				morphNormals: true,
				vertexColors: THREE.NoColors, 
				shading: THREE.SmoothShading, 
				map: new GT.Texture('/textures/brown_eye.png')
		});
		geometry.computeFaceNormals();
		geometry.computeMorphNormals();
		var mesh = new THREE.MorphAnimMesh(geometry, material);
		scene.add(mesh);
		otherPlayers[name] = new GT.Character(name, mesh, camera, [terrain], data);
	});
}

function init() {				
	// SCENE
	scene = new THREE.Scene();
	// CAMERA
	camera = new THREE.PerspectiveCamera(45, screen.w / screen.h, 0.1, 50000);
	camera.position.set(0, 40, -100);
	scene.add(camera);
	// CLAVIER
	keyboard = new GT.Keyboard();
	// RENDERER
	renderer = new THREE.WebGLRenderer({ antialias: true });
	renderer.setSize(screen.w, screen.h);
	$('body')append(renderer.domElement);
	// LIGHT
	var light = new THREE.DirectionalLight(0xffffff, 0.8);
	light.position.set(0, 500, 0).normalize();
	scene.add(light);
	// SKYBOX/FOG
	var skyBoxGeometry = new THREE.CubeGeometry(10000, 10000, 10000);
	var skyBoxMaterial = new THREE.MeshBasicMaterial({ color: 0x9999ff, side: THREE.BackSide });
	var skyBox = new THREE.Mesh(skyBoxGeometry, skyBoxMaterial);
	scene.add(skyBox);
	//scene.fog = new THREE.FogExp2( 0x9999ff, 0.00025 );
	// LIGHT
	var ambientLight = new THREE.AmbientLight(0x111111);
	scene.add(ambientLight);
	// EVENTS
	$(window).resize(function(event) {
		event.preventDefault();
		screen.w  = window.innerWidth;
		screen.h = window.innerHeight;
		renderer.setSize(screen.w, screen.h);
		camera.aspect = screen.w / screen.h;
		camera.updateProjectionMatrix();
	});

	// Loader JSON
	var loader = new THREE.JSONLoader();
	// TERRAIN
	loader.load('/models/map1.js', function(geometry) {
		var material = new THREE.MeshPhongMaterial({
			vertexColors: THREE.NoColors, shading: THREE.SmoothShading, map: new GT.Texture('/textures/map1.png')
		});
		var mesh = new THREE.Mesh(geometry, material);
		scene.add(mesh);
		terrain = new GT.Terrain(mesh);
		// Loader pour le joueur
		var loaderChar = new THREE.JSONLoader();
		// MAIN CHARACTER
		loaderChar.load('/models/male.js', function(geometry) {
			var material = new THREE.MeshPhongMaterial(
				{ color: 0xffffff, specular: 0x333333, shininess: 20, wrapAround: true, morphTargets: true, morphNormals: true,
				vertexColors: THREE.NoColors, shading: THREE.SmoothShading, map: new GT.Texture('/textures/male_texture.png') },
				{ color: 0xffffff, specular: 0x333333, shininess: 20, wrapAround: true, morphTargets: true, morphNormals: true,
				vertexColors: THREE.NoColors, shading: THREE.SmoothShading, map: new GT.Texture('/textures/green_eye.png') });
			geometry.computeFaceNormals();
			geometry.computeMorphNormals();
			var mesh = new THREE.MorphAnimMesh(geometry, material);
			scene.add(mesh);
			player = new GT.Character(pseudo, mesh, camera, [terrain], {
				position: {x: 0, y: 0, z: 0},
				rotation: {x: 0, y: 0, z: 0},
				animation: null
			});
		});
	});
	// GUI
	initGUI();
}

function initGUI() {
	gui = new GT.GUI();
	// Menu
	for (var i = 0; 10 > i; i++) {
		var menu = new GT.Menu('sort' + i, '50px', '50px', 'sorts');
		menu.css('left', parseInt(60 + i * 61) + 'px');
		gui.add(menu);
	}
	// Minimap
	minimap = new GT.Minimap('/textures/map1.png', {'width': 512, 'height': 512}, '150px', '150px');
	gui.add(minimap);
	// Image du joueur
	var hplayer = new GT.Menu('myhead', '50px', '50px', 'player');
	gui.add(hplayer);
	// Barre de vie
	var playerhealth = new GT.Menu('myhealth', '150px', '15px', 'barres');
	playerhealth.css('top', '15px');
	playerhealth.css('background-color', '#EF0000');
	gui.add(playerhealth);
	// Barre de mana
	var playermana = new GT.Menu('mymana', '150px', '15px', 'barres');
	playermana.css('top', '35px');
	playermana.css('background-color', '#0000EF');
	gui.add(playermana);
	// Chat
	chatbox = new GT.Chat(keyboard, pseudo, socket);
	chatbox.load();
}

function animate() {
	requestAnimationFrame(animate);
	render();
	update();
}

function update() {
	// delta = change in time since last call (seconds)
	delta = clock.getDelta();
	if (player) {
		if (terrain) {
			var direction = keyboard.keyDirection();
			direction.multiplyScalar(delta);
			if (direction.z != 0) {
				// Animation : walk
				player.setAnimation(GT.walk);
				socket.emit('animate_joueur', GT.walk);
			} else {
				player.setAnimation(null);
				socket.emit('animate_joueur', null);
			}
			if (direction.x != 0 || direction.z != 0) {
				// Deplacement
				socket.emit('move_joueur', {x: direction.x, y: direction.y, z: direction.z});
				player.move(direction);
				// Nouvelle position
				socket.emit('position_joueur', {x: player.mesh.position.x, y: player.mesh.position.y, z: player.mesh.position.z});
			}
			var rotation = keyboard.keyDirection();
			rotation.multiplyScalar(delta);
			if (rotation.y != 0) {
				// Rotation
				socket.emit('rotate_joueur', {x: rotation.x, y: rotation.y, z: rotation.z});
				player.rotate(rotation);
				// Nouvelle rotation
				socket.emit('rotation_joueur', {x: player.mesh.rotation.x, y: player.mesh.rotation.y, z: player.mesh.rotation.z});
			}
		}
		player.update();
	}
	gui.update(terrain, player);
}

function render() {
	if (player) {
		player.animate();
	}
	$.each(otherPlayers, function(key, item) {
		if (otherPlayers[key]) {
			otherPlayers[key].animate();
		}
	});
	renderer.render(scene, camera);
}
